package org.mybatis.generator.codegen.mybatis3;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.VisitableElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/*********************************************************
 * 文件名称：SuffixUtil.java
 * 系统名称：cxycloud
 * 模块名称：org.mybatis.generator.codegen.mybatis3.javamapper.elements.suffix
 * 功能说明：
 * 开发人员 @author：caoxy
 * 开发时间 @date：2023/8/28 10:37
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class SuffixUtil {
    public static final String SUFFIX = "suffix";

    public static final String SUFFIXS = SUFFIX + "S";

    public static final String ENABLE_SUFFIX = "enableSuffix";

    public static final String HOLD_OLD = "holdOld";

    public static final String SUFFIX_SPLIT = "suffixSplit";

    public static String getSuffixMethodName(String methodName,IntrospectedTable introspectedTable){
        if (SuffixUtil.isHoldOld(introspectedTable)){
            methodName += "Suffix";
        }
        return methodName;
    }
    public static String addTableNameSuffix(String tableName,IntrospectedTable introspectedTable){
        String suttfiSplit = Optional.ofNullable(introspectedTable.getTableConfigurationProperty(SuffixUtil.SUFFIX_SPLIT)).orElse("");
        String ifstr1 = "<if test=\"tableSuffix!=''.toString()\">" + tableName + suttfiSplit +  "${tableSuffix}" + "</if>";
        String ifstr2 = "<if test=\"tableSuffix==''.toString()\">" + tableName + "</if>";
        return ifstr1 + ifstr2 ;
    }
    public static VisitableElement getForeachSuffix(VisitableElement... elements){
        XmlElement foreachElement = new XmlElement("foreach");
        foreachElement.addAttribute(new Attribute("collection", "tableSuffixList"));
        foreachElement.addAttribute(new Attribute("item", "tableSuffix"));
        foreachElement.addAttribute(new Attribute("separator", "union all"));
        for (VisitableElement visitableElement : elements) {
            foreachElement.addElement(visitableElement);
        }
        return foreachElement;
    }
    public static boolean isEnableSuffix(IntrospectedTable introspectedTable){
        String enableSuffix = introspectedTable.getTableConfigurationProperty(SuffixUtil.ENABLE_SUFFIX);
        return enableSuffix != null && enableSuffix.equals("true");
    }
    public static boolean isHoldOld(IntrospectedTable introspectedTable){
        String holdOld = introspectedTable.getTableConfigurationProperty(SuffixUtil.HOLD_OLD);
        return holdOld != null && holdOld.equals("true");
    }
    /**
     * 增加后缀list的包引入与参数
     * @param importedTypes
     * @param method
     */
    public static void addSuffixListParam(Set<FullyQualifiedJavaType> importedTypes, Method method) {
        /*增加包引入与参数*/
        importedTypes.add(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param"));
        FullyQualifiedJavaType parameterType = FullyQualifiedJavaType.getNewListInstance();
        FullyQualifiedJavaType listType = FullyQualifiedJavaType.getStringInstance();
        importedTypes.add(parameterType);
        importedTypes.add(listType);
        parameterType.addTypeArgument(listType);
        method.addParameter(new Parameter(parameterType, "tableSuffixList", "@Param(\"tableSuffixList\")"));
    }
    public static void addSuffixParam(Set<FullyQualifiedJavaType> importedTypes, Method method) {
        /*增加包引入与参数*/
        importedTypes.add(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param"));
        FullyQualifiedJavaType parameterType = FullyQualifiedJavaType.getStringInstance();
        importedTypes.add(parameterType);
        method.addParameter(new Parameter(parameterType, "tableSuffix", "@Param(\"tableSuffix\")"));
    }

    /**
     * 删除原方法
     * @param interfaze
     * @param method
     */
    public static void deleteOldMethod(Interface interfaze, Method method) {
        List<Method> methods = interfaze.getMethods();
        List<Method> removeList = methods.stream()
                .filter(interfazeMethod -> interfazeMethod.getName().equals(method.getName()))
                .collect(Collectors.toList());
        methods.removeAll(removeList);

    }
    public static void deleteOldElement(XmlElement parent,XmlElement sub){
        String attrId = getXmlElementAttrId(sub);
        List<VisitableElement> elements = parent.getElements();
        List<VisitableElement> removeList = elements.stream()
                .filter(xmlElement -> xmlElement instanceof XmlElement &&
                        attrId.equals(getXmlElementAttrId((XmlElement) xmlElement)))
                .collect(Collectors.toList());
        elements.removeAll(removeList);
    }
    public static String getXmlElementAttrId(XmlElement xmlElement){
        for (Attribute attribute : xmlElement.getAttributes()) {
            if (attribute.getName().equals("id")){
                return attribute.getValue();
            }
        }
        return null;
    }
}
